#include "common.h"
#include <time.h>

#define MAIN_FUNCTION 4

#define WINDOW1 "handpie window 1"
#define WINDOW2 "handpie window 2"
#define WINDOW3 "handpie window 3"

void mainSangin(LPBYTE lpInput, LPWORD lpHue, LPBYTE lpSat, LPBYTE lpInt, int width, int height);

int main(int argc, char** argv) {
	// OpenCV variables
	CvCapture* capture;
	IplImage* image;  // input frame
	IplImage* output1;  // output frame
	IplImage* output2;  // output frame
	IplImage* output3;  // output frame

	// Image buffers
	LPWORD imageRgb565;
	LPWORD imageHue;
	LPBYTE imageSaturation;
	LPBYTE imageIntensity;

	/*
	* temporary variables
	*/
	int resolution;
	int i, j, index;
	WORD currentHue;
	BYTE currentSat;
	BYTE currentInt;

	/*
	* running time measuring variables
	*/
	clock_t startTime = 0L;
	double elapsedTime = 0;
	char frames = 0;
	int timing = 0;

	/***************
	* main start
	***************/
	capture = cvCaptureFromCAM(0);

	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, WIDTH);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
	cvNamedWindow(WINDOW1, CV_WINDOW_AUTOSIZE);
	cvNamedWindow(WINDOW2, CV_WINDOW_AUTOSIZE);
	cvNamedWindow(WINDOW3, CV_WINDOW_AUTOSIZE);

	if (!cvGrabFrame(capture)) {
		printf("camera is NUll");
	}
	image = cvRetrieveFrame(capture);
	output1 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	output2 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	output3 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);

	/*
	* memory allocation
	*/
	resolution = image->width * image->height;
	imageRgb565 = (LPWORD) malloc(sizeof(WORD) * resolution);
	imageHue = (LPWORD) malloc(sizeof(WORD) * resolution);
	imageSaturation = (LPBYTE) malloc(sizeof(BYTE) * resolution);
	imageIntensity = (LPBYTE) malloc(sizeof(BYTE) * resolution);

	/*
	* processing each frames
	*/
	while (cvWaitKey(10) < 0) {
		startTime = clock();

		cvGrabFrame(capture);
		image = cvRetrieveFrame(capture);

		// convert rgb to hsi
		//rgb888to565((LPBYTE) image->imageData, imageRgb565, image->width, image->height);
		//rgb2hsi(imageRgb565, imageHue, imageSaturation, imageIntensity, image->width, image->height);
		rgb888toHsi((LPBYTE) image->imageData, imageHue, imageSaturation, imageIntensity, image->width, image->height);

		// 여기에 자신의 mainName() 함수를 사용하면 됨
#if MAIN_FUNCTION==0
#elif MAIN_FUNCTION==1
#elif MAIN_FUNCTION==2
#elif MAIN_FUNCTION==3
#elif MAIN_FUNCTION==4
		mainSangin((LPBYTE) image->imageData, imageHue, imageSaturation, imageIntensity, image->width, image->height);
#endif
		output2->imageData = imageHue;
		output3->imageData = imageSaturation;

		cvShowImage(WINDOW1, image);
		cvShowImage(WINDOW2, output2);
		cvShowImage(WINDOW3, output3);

		// fps 계산
		frames++;
		elapsedTime = (elapsedTime + ((double) (clock() - startTime) / CLOCKS_PER_SEC));
		if (elapsedTime >= 1.0) {
			printf("fps: %d\n", frames);
			elapsedTime -= 1.0;
			frames = 0;
		}
	}

	/*
	* release allocation & resources
	*/
	free(imageRgb565);
	free(imageHue);
	free(imageSaturation);
	free(imageIntensity);
	cvReleaseImage(&output1);
	cvReleaseImage(&output2);
	cvReleaseImage(&output3);
	cvReleaseCapture(&capture);
	cvDestroyWindow(WINDOW1);
	cvDestroyWindow(WINDOW2);
	cvDestroyWindow(WINDOW3);

	return 0;
}

void mainSangin(LPBYTE lpInput, LPWORD lpHue, LPBYTE lpSat, LPBYTE lpInt, int width, int height) {
}